//
//  NBHomeTableViewController.swift
//  NBSinaWeibo
//
//  Created by ac on 16/5/19.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit
import SVProgressHUD

private let cellReuseIdentifier = "home"

class NBHomeTableViewController: NBBaseTableViewController {

//    private lazy var statusArr = [NBStatus]()
    private lazy var statusList = NBStatusListViewModel()
    
    private lazy var pullupView: UIActivityIndicatorView = {
        let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        //初始的时候做动画
        return indicatorView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavItem()
        if !NBUserAccountViewModel.sharedViewModel.isUserLogin {
            setupLoginView()
           
            return
        }
        
        
        prepareForTableView()
        loadData()
        
        
    }
    
    private func setNavItem(){
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(pushDetailVC))
    }
    
    private func setupLoginView() -> () {
        loginView.updateUI("visitordiscover_feed_image_smallicon", lblText: "关注一些人,回到这里看看有什么惊喜", isHome: true)
    }
    
    private func prepareForTableView(){
        tableView.registerClass(NBHomeTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
//        tableView.rowHeight = 300
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.separatorStyle = .None
        tableView.tableFooterView = pullupView
        
        let refreshView = NBRefreshControl()
        self.tableView.addSubview(refreshView)
        
//        refreshControl = UIRefreshControl()
//        refreshControl?.addTarget(self, action: #selector(NBHomeTableViewController.loadData), forControlEvents: .ValueChanged)
        
    }
    
    @objc private func loadData(){
        statusList.loadData(pullupView.isAnimating()) { (isSuccess) in
            self.pullupView.stopAnimating()
            if isSuccess{
                self.refreshControl?.endRefreshing()
                self.tableView.reloadData()
            }else{
                SVProgressHUD.showErrorWithStatus(errorAlert)
            }
            
            
        }
    }
    
    @objc private func pushDetailVC(){
        let detailVC = NBDetailViewController()
        detailVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
    

}

extension NBHomeTableViewController{
    // MARK: - Table view data source
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return statusList.statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier, forIndexPath: indexPath) as! NBHomeTableViewCell
        
        // //如果正在加载最后一行cell的时候 && 没有转动的时候 开始加载老数据
        if indexPath.row == statusList.statuses.count - 1 && !pullupView.isAnimating() {
            print("开始执行静默加载")
            pullupView.startAnimating()
            
            //加载数据
            loadData()
        }

        
        cell.statusVM = statusList.statuses[indexPath.row]
//        print(statusList.statuses[indexPath.row].status!.retweeted_status?.text)
        return cell
        
    }
}
